class Utils {
	public constructor() {

	}

	//封装一个aabb碰撞检测给自己来使用
	static hitTest(obj1,obj2) {
		let P1 = obj1.localToGlobal(0,0)
		// console.log(P1.y)
		let P2 = obj2.localToGlobal(0,0)
		if(P1.x+obj1.width>P2.x && P2.x+obj2.width>P1.x && P1.y+obj1.height>P2.y && P2.y+obj2.height>P1.y) {
			return true;
		}
		return false;
	}

	static creatImg(imgName,x=0,y=0){
		let img = new egret.Bitmap();
		img.x = x;
		img.y = y;
		img.texture = RES.getRes(imgName);
		return img;
	}

	static createRect(x,y,w,h,bgColor,lineW=0,lineColor=0x000000){
		let shape = new egret.Shape();
		shape.graphics.lineStyle(lineW,lineColor);
		shape.graphics.beginFill(bgColor);
		shape.graphics.drawRect(0,0,w,h);
		shape.x = x;
		shape.y = y;
		shape.graphics.endFill();
		return shape;
	}

	static createTxt(text,x=0,y=0,size=30,color=0x000000,textAlign="center",verticalAlign="middle"){
		let textField = new egret.TextField();
		textField.textColor = color;
		textField.textAlign = textAlign;
		textField.verticalAlign = verticalAlign;
		textField.size = size;
		textField.x = x;
		textField.y = y;
		textField.text = text;
		return textField;
	}

	static creatBtn(x,y,width,height,text,callBack=function(){},fontSize=40,bgColor=0x006699,fontColor=0xCC3366,hoverBgColor=0xFFCC00,hoverFontColor=0x000000){
		let sprite = new egret.Sprite();
		//按钮矩形
		sprite.graphics.beginFill(bgColor);
		sprite.x = x;
		sprite.y = y;
		sprite.graphics.drawRect(0,0,width,height);
		sprite.graphics.endFill();
		//按钮文字
		let txt = new egret.TextField();
		txt.width = width;
		txt.height = height;
		txt.textAlign = egret.HorizontalAlign.CENTER;
		txt.verticalAlign = egret.VerticalAlign.MIDDLE;
		txt.text = text;
		txt.size = fontSize;
		txt.textColor = fontColor;
		sprite.addChild(txt);

		//点击事件
		//sprite.touchChildren = true;
		sprite.touchEnabled = true;
		sprite.addEventListener(egret.TouchEvent.TOUCH_BEGIN,function(){
			sprite.graphics.clear();//先擦除之前的绘制
			this.graphics.beginFill(hoverBgColor);
			this.x = x;
			this.y = y;
			this.graphics.drawRect(0,0,width,height);
			this.graphics.endFill();

			txt.textColor = hoverFontColor;

		},sprite)
		sprite.addEventListener(egret.TouchEvent.TOUCH_END,function(){
			touchEnd.call(sprite);
		},sprite)

		sprite.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE,function(){
			touchEnd.call(sprite);
		},sprite)


		function touchEnd(){
			// console.log(this);
			sprite.graphics.clear();//先擦除之前的绘制
			sprite.graphics.beginFill(bgColor);
			sprite.x = x;
			sprite.y = y;
			sprite.graphics.drawRect(0,0,width,height);
			sprite.graphics.endFill();

			txt.textColor = fontColor;

			typeof callBack == "function" && callBack();
		}
		return sprite;
	}

	static timeFormat(num){
		num = Math.floor(num);
		let sec = num % 60;
		let min = Math.floor(num/60);
		let finSec = sec<10?"0"+sec:sec;
		let finMin = min<10?"0"+min:min;
		return `${finMin}:${finSec}`
	}

	

}